const router = require('koa-router')()
const result = require('../../config/result')
const { getToken, Addurl, TripUrl } = require('../../config/database')
const { checking, regChecking } = require('../../config/checking')
const COS = require('cos-nodejs-sdk-v5')
//注册接口
router.post('/register', async (ctx) => {
  //   const token= new getToken().getToken()
  // let name="马化腾"
  // let query=`db.collection("ceshi").add({data:{naem:'${name}'}})`
  // let res=await new getToken().posteve(Addurl,query)
  // console.log(res)
  //   new result(ctx,"请求成功").answer()
  let { account, password } = ctx.request.body
  new regChecking(ctx, account, password).start()
  //手机号码是否注册过
  const query = `db.collection("business-acc").where({account:'${account}'}).get()`
  try {
    const user = await new getToken().posteve(TripUrl, query)
    // console.log(user)
    if (user.data.length > 0) {
      new result(ctx, '已经注册过', 202).answer()
    } else {
      //没有注册过
      //生成商家唯一标识uid
      const uid = new Date().getTime()
      // console.log(uid)
      const struid = JSON.stringify(uid)
      const OBJ = { account, password, uid: struid }
      const SRT = JSON.stringify(OBJ)
      const addQuery = `db.collection("business-acc").add({data:${SRT}})`
      await new getToken().posteve(Addurl, addQuery)
       new result(ctx, '注册成功').answer()
    }
  } catch (error) {
     new result(ctx, '注册失败,服务器发生错误', 500).answer()
  }
})

module.exports = router.routes()
